<template>
  <div class="users">
    <div style="display: flex; justify-content: space-between">
      <h2>商品分类页面</h2>
      <el-button type="primary" plain @click="openDialog">新增</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="type" label="id" />
      <el-table-column prop="title" label="标题" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="warning"
            size="small"
            plain
            @click="openEditDialog(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="confirm(scope.row)">
            <template #reference>
              <el-button type="danger" size="small" plain> 删除 </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增和编辑的弹出框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500">
      <el-form :model="form" label-width="auto" style="max-width: 600px">
        <el-form-item label="分类名">
          <el-input v-model="form.title" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="add"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import {
  getClassifyApi,
  removeClassifyApi,
  addClassifyApi,
  editClassifyApi,
} from "@/request/api";
import { ElMessage } from "element-plus";

interface ItemType {
  type: number;
  title: string;
}
const tableData = ref<ItemType[]>([]);
const getData = async () => {
  const res = await getClassifyApi();
  tableData.value = res.data;
};
onMounted(() => {
  getData();
});

// 删除
const confirm = async (row: ItemType) => {
  const res: any = await removeClassifyApi({ type: row.type });
  ElMessage({
    message: res.msg,
    type: "success",
  });
  getData();
};

// 新增
const dialogVisible = ref<boolean>(false);
const dialogTitle = ref<string>("新增分类");
const editType = ref<number>(0);
const openDialog = () => {
  dialogVisible.value = true;

  // 点新增的时候需要清空输入框
  form.title = "";
  dialogTitle.value = "新增分类";
};
const openEditDialog = (row: ItemType) => {
  dialogVisible.value = true;

  // 点编辑的时候需要将对应的数据填到输入框
  form.title = row.title;
  editType.value = row.type;
  dialogTitle.value = "修改分类";
};
const form = reactive({
  title: "",
});
const add = async () => {
  if (dialogTitle.value === "新增分类") {
    const res = await addClassifyApi({ title: form.title });
    ElMessage({
      message: res.msg,
      type: "success",
    });
    dialogVisible.value = false;
    getData();
  } else {
    // 调编辑接口
    const res = await editClassifyApi({
      title: form.title,
      type: editType.value,
    });
    ElMessage({
      message: res.msg,
      type: "success",
    });
    dialogVisible.value = false;
    getData();
  }
};
</script>

<style lang="less">
h2 {
  font-size: 24px;
  margin-bottom: 20px;
}
</style>
